package com.mi.fastbird.common.spring.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 全局拦截器
 *
 * @author 王永吉
 * @date 2019/8/21
 */
@Slf4j
@Component
public class GlobalInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object object) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type,Token,Auth");
        //初始化项目和数据库之后，放开此处 关闭权限认证，登录系统管理员角色，进行角色菜单分配
        //        分配后，注释此处，开启权限认证
        //        if (1 == 1) {
        //            return true;
        //        }

        //OPTIONS 处理
        if (request.getMethod().equals("OPTIONS")) return true;

        String requestURI = request.getRequestURI();
        // 缺少检查权限注解
        if (requestURI.contains("/public/") || requestURI.contains("static/") || requestURI.contains("/static/")) {
            log.info("直接放行 ：" + requestURI);
            return true;
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object,
                                Exception exception) {
        //        RepeatedSubmit.remove(UserKit.uId(), ApiKit.request().getRequestURI());
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object,
                           ModelAndView modelAndView) {
    }

}
